package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.Pay_DanYICZBean;
import com.kucai.yifaqapp.util.MyWebviewUtls;
import com.kucai.yifaqapp.util.SYSOUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by admin on 2018/12/4.
 */

public class Z_WXctivity extends BaseActivitySimple {
    private EditText kj001;
    private TextView kj002;
    private Spinner kj003;
    private TextView kj004;
    private Button kj005;
    private List<String> mdatas = new ArrayList<>();
    Pay_DanYICZBean bean;
    RelativeLayout back;
    private String czid;
    LinearLayout skinbg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.z_wx);
        skinbg = findViewById(R.id.skinbg);
        initview();
        initData();
    }

    @Override
    protected int setContentView() {
        return R.layout.z_wx;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    private void initData() {
        HttpJsonUtils.getHttpApiInstance()
                .getSingleCZ(HttpAdress.getuseAdress() + "api/m/user/getPay","WX", Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_DanYICZBean>() {
                    @Override
                    protected void onCustomNext(Pay_DanYICZBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                bean = resBanner;
                                for (int i = 0; i < resBanner.getRech_types().size(); i++) {
                                    mdatas.add(resBanner.getRech_types().get(i).getDsplay());
                                }
                                //给spinner设置adapter
                                ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(Z_WXctivity.this,
                                        R.layout.item_selected, mdatas);
                                spinnerAdapter.setDropDownViewResource(R.layout.item_selected);
                                kj003.setAdapter(spinnerAdapter);
                                kj003.setDropDownWidth(600); //下拉宽度
                                kj003.setDropDownVerticalOffset(1); //下拉的纵向偏移
                                kj003.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                        TextView tv = (TextView) view;
                                        tv.setTextColor(getResources().getColor(R.color.white));    //设置颜色
                                        tv.setTextSize(13.0f);    //设置大小
                                        tv.setGravity(Gravity.CENTER_VERTICAL);   //设置居中
                                        tv.setPadding(50, 0, 0, 0);
                                        kj002.setText("单笔下限" + bean.getRech_types().get(position).getMin_money() + "单笔上限" + bean.getRech_types().get(position).getMax_money());
                                        kj004.setText(bean.getRech_types().get(position).getRemark() + "");
//                                z_bankTransfer_ty.min=bean.getRech_types().get(position).getMin_money()+"";
                                        czid = bean.getRech_types().get(position).getId() + "";
                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parent) {
                                    }
                                });
                            } else {
                                Toast.makeText(Z_WXctivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(Z_WXctivity.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    private void initview() {
        kj001 = findViewById(R.id.kj001);
        kj002 = findViewById(R.id.kj002);
        kj003 = findViewById(R.id.kj003);
        kj004 = findViewById(R.id.kj004);
        kj005 = findViewById(R.id.kj005);
        back = findViewById(R.id.back);
        kj005.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //直接跳转h5界面
                if (kj001.getText().toString().isEmpty()) {
                    Toast.makeText(Z_WXctivity.this, "请输入金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                String url = HttpAdress.getuseAdress() + "api/m/pay/paymentSubmission" + "?id=" + czid + "&money=" + kj001.getText().toString() + "&token=" + Info.user.token;
                Intent intent = new Intent(Z_WXctivity.this, MyWebviewUtls.class);
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
